package com.devbobcorn.donutopia.client.event;

import com.devbobcorn.donutopia.DonutColors;
import com.devbobcorn.donutopia.Donutopia;
import com.devbobcorn.donutopia.blocks.ModBlocks;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.world.DimensionRenderInfo;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DeferredWorkQueue;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

// Client-Side Only Things...
@EventBusSubscriber(modid = Donutopia.MODID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ClientModEventSubscriber {
    private static final Logger LOGGER = LogManager.getLogger(Donutopia.MODID + " Client Event Subscriber");

    @SubscribeEvent
    @OnlyIn(Dist.CLIENT)
    @SuppressWarnings({"deprecation","unchecked"})
	public static void onClientSetupEvent(FMLClientSetupEvent event) {
		LOGGER.info("Client Side Setup.");

		RenderType transparentRenderType = RenderType.cutoutMipped();
		//RenderType cutoutRenderType = RenderType.cutout();
		//RenderType translucentRenderType = RenderType.translucent();
    
        RenderTypeLookup.setRenderLayer(ModBlocks.VELVET_GRASS_BLOCK.get(), transparentRenderType);
        RenderTypeLookup.setRenderLayer(ModBlocks.MILKY_VELVET_GRASS_BLOCK.get(), transparentRenderType);
        RenderTypeLookup.setRenderLayer(ModBlocks.RAINBOW_GRASS_BLOCK.get(), transparentRenderType);
        RenderTypeLookup.setRenderLayer(ModBlocks.AURORA_LEAVES.get(), transparentRenderType);
        RenderTypeLookup.setRenderLayer(ModBlocks.SAPPHIRE_LEAVES.get(), transparentRenderType);
        RenderTypeLookup.setRenderLayer(ModBlocks.RAINBOW_LEAVES.get(), transparentRenderType);

        DimensionRenderInfo customEffect = new DimensionRenderInfo(128, true, DimensionRenderInfo.FogType.NORMAL, false, false) {
			@Override
			public Vector3d getBrightnessDependentFogColor(Vector3d color, float sunHeight) {
				return new Vector3d(0.4, 0, 0.8);
			}

			@Override
			public boolean isFoggyAt(int x, int y) {
                // TODO: Foggy In some biomes...
				return false;
			}
		};
		DeferredWorkQueue.runLater(() -> {
			try {
				Object2ObjectMap<ResourceLocation, DimensionRenderInfo> effectsRegistry = (Object2ObjectMap<ResourceLocation, DimensionRenderInfo>) ObfuscationReflectionHelper
					.getPrivateValue(DimensionRenderInfo.class, null, "field_239208_a_");
				effectsRegistry.put(new ResourceLocation(Donutopia.MODID, "donutopia"), customEffect);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
    }

    @SubscribeEvent
	public static void registerBlockColors(ColorHandlerEvent.Block event) {
		event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getLoopColorAt(pos.getY(), DonutColors.pink1, DonutColors.pink2) : 8562943;
        }, ModBlocks.VELVET_GRASS_BLOCK.get());

        event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getLoopColorAt(pos.getY(), DonutColors.blue1, DonutColors.blue2) : 8562943;
        }, ModBlocks.MILKY_VELVET_GRASS_BLOCK.get());

        event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getRainbowColorAt(pos.getY()) : 8562943;
        }, ModBlocks.RAINBOW_GRASS_BLOCK.get());

		event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getLoopColorAt(pos.getY(), DonutColors.pink1, DonutColors.pink3) : 8562943;
        }, ModBlocks.AURORA_LEAVES.get());

		event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getLoopColorAt(pos.getY(), DonutColors.blue1, DonutColors.blue3) : 8562943;
        }, ModBlocks.SAPPHIRE_LEAVES.get());

        event.getBlockColors().register((state, view, pos, tintIndex) -> {
            return view != null && pos != null ? DonutColors.getRainbowColorAt(pos) : 8562943;
        }, ModBlocks.RAINBOW_LEAVES.get());
    }

    @SubscribeEvent
	public static void registerItemColors(ColorHandlerEvent.Item event) {
        event.getItemColors().register((stack, index) -> {
            return DonutColors.getItemColor(stack, DonutColors.pink1, DonutColors.pink2);
        }, ModBlocks.VELVET_GRASS_BLOCK.get());

        event.getItemColors().register((stack, index) -> {
            return DonutColors.getItemColor(stack, DonutColors.blue1, DonutColors.blue2);
        }, ModBlocks.MILKY_VELVET_GRASS_BLOCK.get());

        event.getItemColors().register((stack, index) -> {
            return DonutColors.getRainbowItemColor(stack);
        }, ModBlocks.RAINBOW_GRASS_BLOCK.get());

        event.getItemColors().register((stack, index) -> {
            return DonutColors.getItemColor(stack, DonutColors.pink1, DonutColors.pink2);
        }, ModBlocks.AURORA_LEAVES.get());

        event.getItemColors().register((stack, index) -> {
            return DonutColors.getItemColor(stack, DonutColors.blue1, DonutColors.blue2);
        }, ModBlocks.SAPPHIRE_LEAVES.get());

        event.getItemColors().register((stack, index) -> {
            return DonutColors.getRainbowItemColor(stack);
        }, ModBlocks.RAINBOW_LEAVES.get());
    }
}
